﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DatabaseInterface;
using BaseUnits;

namespace Carbon_Tracker
{
    public partial class frmPowerUnitManager : Form
    {
        IPowerUnit powerUnit;
        Connection cnx;
        bool modifyMode;
        public frmPowerUnitManager(Connection cnx, IPowerUnit powerUnit = null)
        {
            this.powerUnit = powerUnit;
            this.cnx = cnx;
            this.modifyMode = false;
            InitializeComponent();
        }

        private void frmPowerUnitManager_Load(object sender, EventArgs e)
        {
            BaseUnits.Builders.PowerPlantBuilder powerPlantBuilder = new BaseUnits.Builders.PowerPlantBuilder(this.cnx);
            List<IPowerPlant> powerPlants = powerPlantBuilder.getPowerPlants();
            int itemIndex;
            foreach (IPowerPlant p in powerPlants)
            {
                itemIndex = this.cmbPowerPlants.Items.Add(p);
            }
            if (this.powerUnit != null)
            {
                this.modifyMode = true;
                this.txtID.Text = this.powerUnit.ID.ToString();
                this.txtExternalID.Text = this.powerUnit.ExternalId;
                this.txtDescription.Text = this.powerUnit.Description;
                this.dtpBuildDate.Text = this.powerUnit.BuildDate.ToShortDateString();
                this.chkCDM.Checked = this.powerUnit.IsCDM;
                this.txtPriority.Text = this.powerUnit.DefaultPriority.ToString();
                this.txtPriority.Enabled = false;
                IPowerPlant powerPlant = this.powerUnit.GetPowerPlant();
                if (powerPlant != null)
                {
                    foreach (IPowerPlant p in powerPlants)
                    {
                        if (p.PowerPlantId == powerPlant.PowerPlantId)
                        {
                            this.cmbPowerPlants.SelectedItem = p;
                        }
                    }
                }
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                IPowerUnit newPowerUnit;
                String externalId = this.txtExternalID.Text;
                String description = this.txtDescription.Text;
                DateTime buildDate = this.dtpBuildDate.Value;
                Boolean isCDM = this.chkCDM.Checked;
                int defaultPriority = Convert.ToInt32(this.txtPriority.Text);
                int powerUnitId;
                BaseUnits.Builders.PowerUnitBuilder builder = new BaseUnits.Builders.PowerUnitBuilder(this.cnx);

                if (this.txtID.Text == "")
                {
                    powerUnitId = -1;
                }
                else
                {
                    powerUnitId = Convert.ToInt32(this.txtID.Text);
                }

                newPowerUnit = new BaseUnits.PowerUnit(powerUnitId, description, externalId, buildDate, null, null, null, isCDM);

                if (this.cmbPowerPlants.SelectedItem == null)
                {
                    throw new Exception("A Power Plant must be selected for this Power Unit");
                    //"Error creating Power Unit", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
                }
                
                int newPowerUnitId = builder.PersistPowerUnit(newPowerUnit);
                              
                if (powerUnitId == -1)
                {
                    newPowerUnit.ID = newPowerUnitId;
                }
                int powerPlantId = ((IPowerPlant)this.cmbPowerPlants.SelectedItem).PowerPlantId;

                //((IPowerPlant)this.cmbPowerPlants.SelectedItem).AddPowerUnit(newPowerUnit);
                builder.PersistPowerPlantAsociation(powerUnitId, powerPlantId);
                
                if (modifyMode)
                {
                    MessageBox.Show("Power unit successfully modified", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    DatabaseInterface.DataBase.PowerUnits.PowerUnitPriorityDataTable priorityTable = builder.GeneratePriority(newPowerUnit.ID, defaultPriority);
                    builder.PersistPriority(priorityTable);
                    MessageBox.Show("New power unit created successfully", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private DatabaseInterface.DataBase.PowerUnits.PowerUnitPriorityDataTable GeneratePriority(int powerUnitId, int priority)
        {
            DatabaseInterface.DataBase.PowerUnits.PowerUnitPriorityDataTable priorityTable = new DatabaseInterface.DataBase.PowerUnits.PowerUnitPriorityDataTable();
            
            return priorityTable;
        }
        private void toolTip1_Popup(object sender, PopupEventArgs e)
        {

        }
    }
}
